// @ts-nocheck

const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));

export default {
  async vickMapPostMessage({ key, value = null } = {}) {
    const self = this;
    let mapKeyList = [];
    if (self.state.mapType == '2dOne') {
      mapKeyList = ['2d'];
    } else if (self.state.mapType == '3dOne') {
      mapKeyList = ['3d'];
    } else if (self.state.mapType == '2d3dSingle') {
      mapKeyList = ['2d', '3d'];
    } else if (self.state.mapType == '2d3dDual') {
      if (key.includes('cameraZoom') || key == 'cameraPositionGet' || key == 'coordinatesTransform') {
        if (self.vickMapRef.isMouseMove3d == true) {
          mapKeyList = ['3d'];
        } else if (self.vickMapRef.isMouseMove2d == true) {
          mapKeyList = ['2d'];
        } else {
          mapKeyList = ['2d', '3d'];
        }
      } else {
        mapKeyList = ['2d', '3d'];
      }
    } else {
      return null;
    }
    let count = 0;
    let maxTime = 0;
    let returnValue = null;
    const length = mapKeyList.length;
    async function isListThen() {
      maxTime++;
      if (count >= length) {
        return returnValue;
      } else if (maxTime > 240) {
        return null;
      } else {
        await sleep(40);
        return await isListThen();
      }
    }
    for (let i = 0; i < length; i++) {
      const mapKey = mapKeyList[i];
      const promise = self['vick' + mapKey + 'MapPostMessage']({
        from: 'vick' + mapKey + 'MapFather',
        type: 'function',
        key: key,
        value: value
      });
      promise.then((value) => {
        returnValue = value;
        count++;
      });
    }
    return await isListThen();
  },
  baseLayerChange(onlineBaseMap = [], offlineBaseMap = []) {
    const self = this;
    self.setState({
      earthBaseLayerConfig: {
        在线底图数组: onlineBaseMap,
        离线底图数组: offlineBaseMap
      }
    });
    self.mapChange();
  },
  mapChange(mapType = null) {
    const self = this;
    if (mapType == null) {
      self.setState({
        mapType: self.state.mapType,
        baseLayerVisible: {},
        editLayerVisible: {},
        index: -self.state.index
      });
    } else {
      self.setState({
        mapType: mapType,
        baseLayerVisible: {},
        editLayerVisible: {},
        index: -self.state.index
      });
    }
  },
  mapIsMobile() {
    const width = document.documentElement.clientWidth || document.body.clientWidth;
    const height = document.documentElement.clientHeight || document.body.clientHeight;
    return width - height >= 0 ? false : true;
  }
};
